﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;

namespace AirXBMC
{
    public static class Program
    {
        private static XbmcController _xbmc;
        private static AirPlayServer _server;

        private static int _picNr;

        public static void Main(string[] args)
        {
            Debug.Listeners.Add(new ConsoleTraceListener());
            _xbmc = new XbmcController(MySettings.Instance.XbmcAddress);
            _server = new AirPlayServer(MySettings.Instance.ServicePort);
            _server.PlayImage += ServerPlayImage;
            _server.Start();
            _server.DoPublish(String.Format("{0} - {1}", SystemInformation.ComputerName, "XBMC"));
            Console.WriteLine("Server running..");
            Console.ReadLine();
            _server.StopPublish();
            _server.Stop();
            Console.WriteLine("Server stopped.");
        }

        private static void ServerPlayImage(object sender, Image theImage)
        {
            Console.WriteLine("Received picture {0}", _picNr++);
            const string filename = "pic.jpg";
            theImage.Save(filename, ImageFormat.Jpeg);
            _xbmc.PlayImage(Path.GetFullPath(filename));
        }
    }
}
